package com.whc.gulimall.ware.controller;

import com.whc.common.exception.BizCodeEnume;
import com.whc.common.utils.PageUtils;
import com.whc.common.utils.R;
import com.whc.gulimall.ware.entity.WareInfoEntity;
import com.whc.gulimall.ware.exception.NoStockException;
import com.whc.gulimall.ware.service.WareInfoService;
import com.whc.gulimall.ware.vo.WareLockVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;


/**
 * 仓库信息
 *
 * @author wanghaochen
 * @email angaoen@outlook.com
 * @date 2022-03-05 00:09:03
 */
@RestController
@RequestMapping("ware/wareinfo")
public class WareInfoController {

    /**
     * 仓库信息
     */
    @Resource
    private WareInfoService wareInfoService;

    /**
     * 获取到地址运费
     *
     * @param addrId 地址Id
     * @return 统一返回结果
     */
    @GetMapping("/address/fare")
    public R getAddressFareByAddrId(@RequestParam("addrId") Long addrId) {
        return R.ok().setData(wareInfoService.getAddressFareByAddrId(addrId));
    }

    /**
     * 锁库存
     *
     * @param lockVo 订单信息vo
     * @return 统一返回结果 Boolean
     */
    @PostMapping("/lock/ware")
    public R orderLockedStock(@RequestBody WareLockVo lockVo) {
        try {
            return R.ok().setData(wareInfoService.orderLockedStock(lockVo));
        } catch (NoStockException e) {
            return R.error(BizCodeEnume.WARE_STOCK_EXCEPTION);
        }
    }

    /**
     * 查询分页数据
     *
     * @param params 查询条件以及分页条件
     * @return 统一返回结果
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = wareInfoService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        WareInfoEntity wareInfo = wareInfoService.getById(id);
        return R.ok().put("wareInfo", wareInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ware:wareinfo:save")
    public R save(@RequestBody WareInfoEntity wareInfo) {
        wareInfoService.save(wareInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("ware:wareinfo:update")
    public R update(@RequestBody WareInfoEntity wareInfo) {
        wareInfoService.updateById(wareInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ware:wareinfo:delete")
    public R delete(@RequestBody Long[] ids) {
        wareInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
